<template>
  <div>
    <div class="app_card mb-2 pt-4 pb-4">
      <div class="d-flex justify-content-between search-form">
        <div class="flex-grow-1">
          <el-input class="w200px mr-3" v-model.trim="queryParam.name" autocomplete="off" placeholder="课程名称"/>
          <el-select class="w200px mr-3" v-model="queryParam.academyId" placeholder="全部学院">
            <el-option  label="全部学院" :value="null" />
            <el-option v-for="item in academyOption" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
          <el-select class="w200px mr-3" v-model="queryParam.majorId" placeholder="全部专业">
            <el-option  label="全部专业" :value="null" />
            <el-option v-for="item in majorOption"  :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
          <el-button type="primary"  @click="onRefresh()">搜索</el-button>
          <el-button type="primary" plain @click="onReset()">重置</el-button>
        </div>
        <div class="d-flex">
          <el-button type="primary" @click="onAdd()">新建</el-button>
          <el-button type="primary" plain @click="onDeleteMulti()">批量删除</el-button>
        </div>
      </div>
    </div>
    <div class="app_card pt-4  pb-5">
      <el-table stripe size="large" :data="dataOfTable" height="calc(100vh - 300px)" sortable="custom" @sort-change="handleSortChange" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column label="课程名称" prop="name" align="center" />
        <el-table-column label="所属学院" prop="Academy.name" align="center"/>
        <el-table-column label="所属专业" prop="Major.name" align="center" show-overflow-tooltip />
        <el-table-column label="课程简介" prop="remark" align="center" show-overflow-tooltip />

        <el-table-column fixed="right" label="操作" width="120" align="left">
          <template #default="scope">
            <el-button link type="primary" @click="onEditRow(scope.row)">编辑</el-button>
          </template>
        </el-table-column>

      </el-table>
      <div class="mt-2">
        <el-pagination background v-model:currentPage="queryParam.page" v-model:page-size="queryParam.pageSize" :page-sizes="paginationOption.pageSizes" :background="false" :layout="paginationOption.layout" :total="paginationOption.count" @size-change="handleChangeSize" @current-change="handleChangeCurrent" />
      </div>
    </div>
    <EditDialog ref="editDialogRef" @success="loadData()"></EditDialog>
  </div>
</template>

<script setup>
/**
 * 角色组件
 */
import { ref } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import EsAjax from '@/utils/EsAjax';
import ApiUrls from '@/apis/index';
//组件
import EditDialog from './EditDialog.vue';
import { roomTypeList } from '@/apis/model/RoomModel';
import { findValue } from '@/utils/EsCommon';
// ======================================================
//                      变量声明
// ======================================================
const academyOption = ref([]);
const majorOption = ref([]);
// 组件引用
const editDialogRef = ref(null);
// 表格数据
const roomTypeOption = ref(roomTypeList);
const dataOfTable = ref([]);
// 组件可视状态
const visibleState = ref({
  searchForm: true,
})
// 表格查询参数
const queryParam = ref({
  name: '',
  majorId: null,
  academyId: null,
  page: 1,
  pageSize: 10,
})
// 分页栏配置数据
const paginationOption = ref({
  layout: 'total, sizes, prev, pager, next, jumper',
  pageSizes: [10, 20, 50, 100, 500, 1000],
  count: 0,
})
// 表格多选数据
const multipleSelection = ref([]);

// ======================================================
//                      点击操作
// ======================================================

// 点击刷新按钮
const onRefresh = () => {
  queryParam.value.page = 1;
  loadData();
}

//点击重置按钮
const onReset = () => {
  queryParam.value = {
  name: '',
  majorId: null,
  academyId: null,
  page: 1,
  pageSize: 10,
}
  loadData()
}

// 点击新增按钮
const onAdd = () => {
  editDialogRef.value.show(null, '添加');
}



// 点击批量删除按钮
const onDeleteMulti = () => {
  let n = multipleSelection.value.length, id = multipleSelection.value.map(v => v.id).toString();
  if (n) {
    ElMessageBox.confirm("确定要删除 " + n + " 条记录吗？", "警告").then(() => {
      EsAjax.delete(ApiUrls.courseDelete, { data: { id } }).then(res => {
        if (res.data.code !== 200) {
          ElMessage.warning(res.data.msg);
          return;
        }
        ElMessage.success("操作成功");
        loadData();
      }).catch(err => {
        ElMessage.error("网络错误：" + err.message);
      })
    }).catch(() => { })
  } else {
    ElMessage.info("请选择批量数据");
  }

}


// 点击行内编辑按钮
const onEditRow = (row) => {
  editDialogRef.value.show(row.id, '编辑 ', row);
}

// ======================================================
//                      处理事件
// ======================================================

// 处理表格全选
const handleSelectionChange = (val) => {
  console.log(val)
  multipleSelection.value = val
}

// 处理表格排序
const handleSortChange = (column) => {
  queryParam.value.orderby = column.prop + ' ' + (column.order == 'descending' ? 'desc' : 'asc');
  console.log(queryParam.value);
  loadData();
}

// 处理分页尺寸
const handleChangeSize = (val) => {
  console.log(`${val} items per page`);
  queryParam.value.page = 1;
  queryParam.value.pageSize = val;
  loadData();
}

// 处理页码
const handleChangeCurrent = (val) => {
  console.log(`current page: ${val}`)
  queryParam.value.page = val;
  loadData();
}

// ======================================================
//                      处理事件
// ======================================================

const loadData = () => {
  EsAjax.post(ApiUrls.courseList, queryParam.value).then(res => {
    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list, count } = res.data.data
    dataOfTable.value = list;
    paginationOption.value.count = count;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}



const getAcademyList = () => {
  EsAjax.post(ApiUrls.academyList, { params: {} }).then(res => {
    console.log(res)

    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list } = res.data.data
    academyOption.value = list;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}

const getMajorList = () => {
  EsAjax.post(ApiUrls.majorList, { params: {} }).then(res => {
    console.log(res)
    if (res.data.code !== 200) {
      ElMessage.warning(res.data.msg);
      return;
    }
    let { list } = res.data.data
    majorOption.value = list;
  }).catch(err => {
    ElMessage.error(err.message);
  })
}

loadData();
getAcademyList();
getMajorList();
</script>

